package com.example.gyt.controller;

import com.example.gyt.pojo.Commodity;
import com.example.gyt.pojo.Land;
import com.example.gyt.pojo.LandLease;
import com.example.gyt.pojo.PageResult;
import com.example.gyt.service.LandLeaseService;
import com.example.gyt.service.UserService;
import com.example.gyt.util.DateUtil;
import com.example.gyt.util.ResultUtil;
import com.example.gyt.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class LandLeaseJsonController {

    @Autowired
    private LandLeaseService landLeaseService;

    //添加土地租用方法
    @ResponseBody
    @RequestMapping(value = "/member/addLandLease",method = RequestMethod.POST)
    public ResultUtil addLandLease(@Validated @RequestBody LandLease landLease, BindingResult result){
        try {

            //是否为空检验
            if(StringUtils.isEmpty(landLease.getLandId())){
                return ResultUtil.failure("土地id不能为空");
            }
            if(StringUtils.isEmpty(landLease.getLandRenterId())){
                return ResultUtil.failure("土地租用者id不能为空");
            }
            if(StringUtils.isEmpty(landLease.getLandLeaseTime())){
                return ResultUtil.failure("土地租用时间不能为空");
            }
            if(StringUtils.isEmpty(landLease.getLandHirer())){
                return ResultUtil.failure("土地租用联系人不能为空");
            }

            landLease.setId(UuidUtil.getUUID());
            //获取到当前时间
            landLease.setCreateTime(DateUtil.getCurrentTime());

            int count = landLeaseService.insertSelective(landLease);
            if (count == 0){
                return ResultUtil.success("添加租用土地失败");
            }

            return ResultUtil.success("添加租用土地信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }

    }

    //删除一块租用土地
    @ResponseBody
    @RequestMapping(value = "/member/deleteLandLease",method = RequestMethod.POST)
    public ResultUtil deleteLandLease(@Validated @RequestBody LandLease landLease, BindingResult result){
        try {

            if(StringUtils.isEmpty(landLease.getId())){
                return ResultUtil.failure("租用土地ID不能为空");
            }

            int count = landLeaseService.deleteByPrimaryKey(landLease.getId());
            if (count == 0){
                return ResultUtil.failure("删除租用土地失败");
            }

            return ResultUtil.success("删除租用土地成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //租用土地ID查信息
    @ResponseBody
    @RequestMapping(value = "/member/queryLandLease",method = RequestMethod.POST)
    public ResultUtil queryLandLease(@Validated @RequestBody LandLease landLease, BindingResult result){
        try {

            if(StringUtils.isEmpty(landLease.getId())){
                return ResultUtil.failure("租用土地ID不能为空");
            }

            LandLease landLease1 = landLeaseService.selectByPrimaryKey(landLease.getId());
            if (landLease1 == null){
                return ResultUtil.failure("获取租用土地信息失败");
            }

            return ResultUtil.success(landLease1);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //修改租用土地信息
    @ResponseBody
    @RequestMapping(value = "/member/updateLandLease",method = RequestMethod.POST)
    public ResultUtil updateLandLease(@Validated @RequestBody LandLease landLease, BindingResult result){
        try {
            if(StringUtils.isEmpty(landLease.getId())){
                return ResultUtil.failure("租用土地ID不能为空");
            }

            /*if(StringUtils.isEmpty(user.getUserName())){
                user.setUserName(null);
            }

            if(StringUtils.isEmpty(user.getUserBrithday())){
                user.setUserBrithday(null);
            }

            if(StringUtils.isEmpty(user.getUserAddress())){
                user.setUserAddress(null);
            }

            if(StringUtils.isEmpty(user.getUserTel())){
                user.setUserTel(null);
            }*/

            //更新修改时间
            landLease.setUpdateTime(DateUtil.getCurrentTime());

            int count = landLeaseService.updateByPrimaryKeySelective(landLease);
            if (count == 0){
                return ResultUtil.failure("修改租用土地信息失败");
            }

            return ResultUtil.success("修改租用土地信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //集合于租用土地分页查询，所有租用土地查询，租用土地类型查询，租用土地名字模糊查询
    @ResponseBody
    @RequestMapping(value = "/member/allLandLeaseList",method = RequestMethod.POST)
    public ResultUtil allLandLeaseList(@Validated @RequestBody LandLease landLease, BindingResult result){
        try {

            List<LandLease> landLease1 = landLeaseService.getAllLandLeaseList(landLease);
            if (landLease1.size() == 0){
                return ResultUtil.failure("查询失败,无该数据");
            }

            //查询数据有多少条
            int countLike = landLeaseService.queryCountLike(landLease);

            //加载实体将数据返回给用户
            PageResult pageResult = new PageResult();
            if (landLease.getPageResult() != null){
                pageResult.setCurrentPage(landLease.getPageResult().getCurrentPage());
                pageResult.setPageSize(landLease.getPageResult().getPageSize());

            }

            pageResult.setTotalSize(countLike);
            pageResult.setItems(landLease1);

            return ResultUtil.success(pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }
}
